<?php

require_once '../config.php';

$administratorId = operators_get_id();
$operator = database_get_entity('operators');

if (!empty($_GET['updateId'])) {
  $operator = array_merge($operator, operator_get_by_id($_GET['updateId']));
}

if ($_POST) {
  operators_register($_POST['name'], $_POST['email'], $_POST['password'], $administratorId);
  header('location: operators.php');  
}

if (!empty($_GET['removeSiteId'])) {
  operators_remove($_GET['removeOperatorId']);
  header('location: operators.php');
}

$operators = operators_get_all($administratorId);

?>

<?php include dirname(__FILE__) . '/header.php' ?>
<table>
  <tr>
    <td id="Sidebar">
      <h2>Add new operator</h2>
      <form action="" method="POST">

        <div>
          <label>Name</label>
          <input type="text" name="name" value="<?php echo $operator['name'] ?>" />
        </div>
        
        <div>                
          <label>E-Mail</label>
          <input type="text" name="email" value="<?php echo $operator['email'] ?>" />
        </div>
        
        <div>
          <label>Password</label>
          <input type="text" name="password" value="<?php echo $operator['password'] ?>" />
        </div>

        <div>                
          <label>Avatar URL</label>
          <input type="text" name="avatar" value="<?php echo $operator['avatar'] ?>" />
        </div>

        <input type="submit" value="Add" />
        
      </form>
      
    </td>
    <td>
      <h2>Your operators</h2>
      <table class="Dg">
        <?php foreach ($operators as $o): ?>
          <tr>
            <td class="DgCell">
              <a href="?updateId=<?php echo $o['id'] ?>"><?php echo $o['name'] ?></a>
            </td>
            <td class="DgCell StringS IconCell">
              <?php if ($o['id'] != $o['administratorId']): ?>
                <a href="?removeOperatorId=<?php echo $o['id'] ?>"><img src="/common/images/trash_16x16.png" /></a>
              <?php endif ?>
            </td>
          </tr>
        <?php endforeach ?>
      </table>
      <br />
    </td>
  </tr>
</table>

<?php include dirname(__FILE__) . '/footer.php' ?>
